#include <bits/stdc++.h>
using namespace std;
inline int read() {
    int x = 0 , f = 1; char ch = getchar();
    for ( ; !isdigit(ch) ; ch = getchar()) if (ch == '-') f = -1;
    for ( ; isdigit(ch) ; ch = getchar()) x = x * 10 + ch - '0';
    return x * f;
}
const int fac[] = {1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880};
const int aim = 24; 
const int maxCon = 362880;
bool vis[maxCon];
struct data {
    int con[9];
    int hashVal;
    int step;
}begin;
queue <data> Q;
int Cantor(int s[]) {
    int sum = 0;
    for (int i = 1 ; i <= 8 ; i ++) {
        int cnt = 0;
        for (int j = i + 1 ; j <= 8 ; j ++) {
            if (s[i] > s[j]) {
                cnt ++;
            }
        }
        sum += cnt * fac[8 - i];
    }
    return sum + 1;
}
int bfs() {
    Q.push(begin);
    while (Q.size()) {
        data top = Q.front(); Q.pop();

        if (vis[top.hashVal]) {
            continue;
        }
        if (top.hashVal == aim) {
            return top.step;
        }
        vis[top.hashVal] = true;
        top.step ++;
        data next = top;
        for (int i = 1 ; i <= 4 ; i ++) {
            swap(next.con[i] , next.con[i + 4]);
        }
        next.hashVal = Cantor(next.con);
        Q.push(next);
        next = top;
        int up = next.con[4] , dwn = next.con[8];
        for (int i = 4 ; i >= 2 ; i --) {
            next.con[i] = next.con[i - 1];
            next.con[i + 4] = next.con[i + 3];
        }
        next.con[1] = up;
        next.con[5] = dwn;
        next.hashVal = Cantor(next.con);
        Q.push(next);
        next = top;
        int two = next.con[2] , three = next.con[3] , seven = next.con[7] , six = next.con[6];
        next.con[2] = six , next.con[3] = two , next.con[7] = three , next.con[6] = seven;
        next.hashVal = Cantor(next.con);
        Q.push(next);
    }
}

int main() {
    for (int i = 1 ; i <= 8 ; i ++) {
        begin.con[i] = read();
    }
    begin.hashVal = Cantor(begin.con);
    printf("%d\n" , bfs());
}
